package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.em.ConstantEnum;
import com.dongdongshop.pojo.StatusCode;
import com.dongdongshop.pojo.TbUser;
import com.dongdongshop.service.TbUserService;
import com.dongdongshop.util.ShiroUtils;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;

@Controller
@RequestMapping("userShiroController")
public class UserShiroController {
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    @Reference
    private TbUserService tbUserService;
    @Autowired
    private RedisTemplate redisTemplate;
    //登录
    @RequestMapping("loginInfo")
    @ResponseBody
    public StatusCode loginInfo(String username,String password){
        Map<String, Object> map = new LinkedHashMap<>();
        if (username == null && password == null) {
            return StatusCode.build(ConstantEnum.NULL_ERROR);
        }
        //1.获取subject对象
        Subject subject = SecurityUtils.getSubject();
        //2.封装token
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        //3.调用登陆方法
        try {
            subject.login(token);
        } catch (UnknownAccountException e) {
            map.put("messg", "用户名不存在");
            return StatusCode.build(ConstantEnum.ERROR).setT(map);
        } catch (IncorrectCredentialsException e) {
            map.put("messg", "密码错误");
            return StatusCode.build(ConstantEnum.ERROR).setT(map);
        }
        map.put("messg", "登陆成功");
        return StatusCode.build(ConstantEnum.SUCCESS).setT(map);
    }

    //跳转注册页面
    @RequestMapping("toRegister")
    public String toRegister(){
        return "register";
    }

    //发送验证码
    @RequestMapping("registersmsInfo")
    @ResponseBody
    public StatusCode registersmsInfo(String phone){
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("messg","验证码发送成功");
        rocketMQTemplate.convertAndSend("phone",phone);
        return StatusCode.build(ConstantEnum.SUCCESS).setT(map);
    }

    //注册
    @RequestMapping("registerInfo")
    @ResponseBody
    public StatusCode reginsterInfo(TbUser tbUser,String rpassword,String sms){
        Map<String, Object> map = new LinkedHashMap<>();
        //查询判断当前用户名是否存在
        TbUser tbUser1 = tbUserService.selectByUsername(tbUser.getUsername());
        if (tbUser1 != null){
            map.put("messg","用户名已存在");
            return StatusCode.build(ConstantEnum.ERROR).setT(map);
        }
        if (!Objects.equals(tbUser.getPassword(),rpassword)){
            map.put("messg","密码不一致，请检查一遍再提交");
            return StatusCode.build(ConstantEnum.ERROR).setT(map);
        }
        //获取redis中的验证码
        ValueOperations valueOperations = redisTemplate.opsForValue();
        String usersms = (String) valueOperations.get(tbUser.getPhone());
        //判断输入的验证码是否正确
        if (!Objects.equals(sms,usersms)){
            map.put("messg","验证码错误");
            return StatusCode.build(ConstantEnum.ERROR).setT(map);
        }
        //生成六位数盐
        String salt = ShiroUtils.generateSalt(6);
        //撒盐加密
        String md5 = ShiroUtils.encryptPassword("MD5", rpassword, salt, 3);
        tbUser.setPassword(md5);
        tbUser.setSalt(salt);
        tbUser.setCreated(new Date());
        tbUser.setUpdated(new Date());
        //注册信息录入
        Boolean b = tbUserService.registerInfo(tbUser);
        if (!b){
            map.put("messg","注册失败");
            return StatusCode.build(ConstantEnum.ERROR).setT(map);
        }
        map.put("messg","注册成功");
        return StatusCode.build(ConstantEnum.SUCCESS).setT(map);
    }
}
